import gradio as gr


def calculator(num1, operation, num2):
    if operation == "add":
        return num1 + num2
    elif operation == "subtract":
        return num1 - num2
    elif operation == "multiply":
        return num1 * num2
    elif operation == "divide":
        if num2 == 0:
            raise gr.Error("Cannot divide by zero")
        return num1 / num2


if __name__ == '__main__':
    demo = gr.Interface(
        fn=calculator,
        inputs=["number", gr.Radio(["add", "subtract", "multiply", "divide"]), "number"],
        outputs="number",
        live=True,  # live=True参数，只要输入发生变化，结果马上发生改变。
    )

    demo.launch(share=True)
